/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.parts.LoggedData.panels;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.NumberFormat;
import java.util.Currency;
import java.util.Locale;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import researchgrants.parts.LoggedData.LogableNumber;
import researchgrants.parts.LoggedData.LogableObject;

/**
 *
 * @author DOStudent1
 */
public class LoggedDataPanelsCurrency extends LoggedDataPanels implements ActionListener , KeyListener{

    private static final Locale locale=Locale.US;
    private static final int MIN_SIZE=5;
    
    @Override
    public Component getViewComponent() {
        validateDataToDisplay();
        JLabel lblViewData=new JLabel();

        NumberFormat usFormat = NumberFormat.getIntegerInstance(locale);
        LogableNumber logableNumber=(LogableNumber) (getData().value());
        if (logableNumber.isEmpty()) {
            lblViewData.setText("");
            
        } else {
            Long value=logableNumber.getNumber();
            lblViewData.setText(Currency.getInstance(locale).getSymbol() + usFormat.format(value));
        }
        return(lblViewData);
    }

    JTextField fldEditText;

    @Override
    public Component getEditComponent() {
        JPanel pnlComponent=new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
        fldEditText=new JTextField();
        fldEditText.addActionListener(this);
        fldEditText.addKeyListener(this);
        LogableNumber logableNumber=(LogableNumber) (getData().value());
        if (hasDataInitialized()) {
            if (logableNumber.getNumber().toString().length()<MIN_SIZE) {
                fldEditText.setColumns(MIN_SIZE);
            }
            fldEditText.setText(logableNumber.getNumber().toString());
        } else {
            fldEditText.setText(getDefaultValue());
            fldEditText.setColumns(MIN_SIZE);
        }
        JLabel lblCurrencyLabel=new JLabel(Currency.getInstance(locale).getSymbol());
        pnlComponent.add(lblCurrencyLabel);
        pnlComponent.add(fldEditText);
        return(pnlComponent);
    }


    public boolean isValid() {
        boolean allOkay=true;
        String currentText=fldEditText.getText();
        if (!currentText.equals("")) {
            long currentNumber=0;
            try {
                currentNumber=Long.valueOf(currentText);
            } catch (NumberFormatException ex) {
                allOkay=false;
            }
            if (currentNumber<0) {
                allOkay=false;
            }
        }
        return(allOkay);
    }


    
    @Override
    public void actionPerformed(ActionEvent e) {
        
        // sends both valid and invalid events
        ChangeEvent eNew=new ChangeEvent(e.getSource());
        notifyAllChangeListeners(eNew);
    }

    @Override
    public boolean isEmpty() {
        return(fldEditText.getText().equals(""));
    }

    @Override
    public void keyTyped(KeyEvent e) {
        actionPerformed(new ActionEvent(e.getSource(), e.getID(), "Key"));
    }

    @Override
    public void keyPressed(KeyEvent e) {
        actionPerformed(new ActionEvent(e.getSource(), e.getID(), "Key"));
    }

    @Override
    public void keyReleased(KeyEvent e) {
        actionPerformed(new ActionEvent(e.getSource(), e.getID(), "Key"));
    }

    @Override
    public LogableObject getEdittedValue() {
        String text = fldEditText.getText();
        Long number=Long.parseLong(text);
        LogableNumber logableNumber=new LogableNumber().fromNumber(number);
        return(logableNumber);
    }
    
}
